import os, re

def parse(latex_lines):
    number_of_lines = len(latex_lines)
    
    check_lines = []
    
    for x, line in enumerate(latex_lines):
      print x,": ", line
      if line[0] == "%":
          print "    COMMENT: %d %s" % (x, line)
          continue
      l = line.rstrip("\r\n")
      p = 0
      s = 0
      q = []
      while p < len(l):
          if l[p] in (".", "!", "?"):
              q.append( ("sentence", l[s:p+1]))
              s = p + 1
              p = s
          elif l[p] in ("\\","~"):
              if s < p:
                    q.append(("text", l[s:p]))
              s = p
              p+=1
              while( p<len(l) and (l[p].isalpha() or l[p] in ("*","#","\\") )): p+=1
              q.append( ("command", l[s:p]))
              s = p
          elif l[p] in ("{","}","[","]","$"):
              if s < p:
                  q.append(("text", l[s:p]))
              q.append( (l[p],l[p]))
              s = p+1
              p = s
          else:
              p+=1
    
      if s < p:
          q.append(("text", l[s:p]))
    
      z =""    
      for xx in q:
          print " "* 6, xx
          z = z + xx[1]
      #test
      if z != l:
          print "CHECK:"
          check_lines.append(x)
          print len(z), z
          print l.__repr__()
          print len(l), l
    if len(check_lines)>0:
      print "Check Lines", check_lines
files = []
files.extend(["appendix%s.tex" % x for x in ("a", "b", "c", "d")])
files.extend(["ch%d.tex" % x for x in range(1,11)])

print files

for f in files:
    parse(open(f).readlines())
    print f
    raw_input()
